<template>
  <div class="app-container">
    <u-page title="领取记录">
      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="活动" prop="activityId">
            <activity v-model="listQuery.activityId" width="160px"/>
          </el-form-item>
          <el-form-item label="领取时间" prop="searchDate">
            <u-date-time-picker v-model="daterange" @change="changeDate" />
          </el-form-item>
          <el-form-item label="会员卡号" prop="cardNo">
            <el-input v-model="listQuery.cardNo" placeholder="请输入会员卡号" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="OPENID" prop="openid">
            <el-input v-model="listQuery.openid" placeholder="请输入OPENID" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="验证手机" prop="yz_mobile">
            <el-input v-model="listQuery.yz_mobile" placeholder="请输入验证手机" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="权益码" prop="cdkey">
            <el-input v-model="listQuery.cdkey" placeholder="请输入权益码" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item label="券号" prop="couponNo">
            <el-input v-model="listQuery.couponNo" placeholder="请输入券号" clearable style="width: 170px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="hdlFilter">搜索</el-button>
            <el-button type="primary" icon="el-icon-download" @click="handleDownload">导出</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>
      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :pagination.sync="listQuery"
        :fetch="hdlList"
        @selection-change="hdlSelectChange"
      >
      </u-table>
    </u-page>
  </div>
</template>

<script>
import activity from '../components/activity'
export default {
  components: {
    activity
  },
  data() {
    return {
      api: this.$u.api.PluginsCdkeyLog,
      daterange: [],
      orderShow: false,
      listQuery: {
        page: 1,
        pageSize: 10,
        totalCount: 1,
        beginTime: '',
        endTime: ''
      },
      // 表格
      columns: [
        {
          prop: 'activityName',
          label: '活动名称',
          render: (h, params) => {
            return h('div', params.row.cdkeyActivity.name)
          }
        },
        {
          prop: 'activityType',
          label: '活动类型',
          render: (h, params) => {
            if (params.row.cdkeyActivity.type === 1) {
              return h('el-tag', { props: { type: 'success' }}, '权益码')
            } else if (params.row.cdkeyActivity.type === 2) {
              return h('el-tag', { props: { type: 'danger' }}, '口令码')
            } else {
              return h('el-tag', { props: { type: 'warning' }}, '手机验证')
            }
          }
        },
        {
          prop: 'cdkey',
          label: '权益码'
        },
        {
          prop: 'cardNo',
          label: '会员卡号'
        },
        {
          prop: 'openid',
          label: 'OPENID'
        },
        {
          prop: 'yz_mobile',
          label: '验证手机'
        },
        {
          prop: 'couponNo',
          label: '券号',
          render: (h, params) => {
            const { couponNo } = params.row
            const couponNoList = couponNo.split(',')
            return h('div', couponNoList.map(item => h('div', [
              h('el-tag', item),
              h('br')
            ])))
          }
        },
        {
          prop: 'createdAt',
          label: '领取时间',
          sortable: true,
          timestamp: true
        }
      ],
      listLoading: false,
      list: [],
      orderList: [],
      listOptions: {
        stripe: true
      }
    }
  },
  created() {
    this.hdlList()
  },
  methods: {
    changeDate(item) {
      if (item) {
        this.listQuery.beginTime = item[0]
        this.listQuery.endTime = item[1]
      } else {
        this.listQuery.beginTime = ''
        this.listQuery.endTime = ''
      }
    },
    handleDownload() {
      // 复制值
      const list = this.$u.deepClone(this.list)
      // 业务数据处理
      list.forEach(item => {
        this.$set(item, 'activityName', item.cdkeyActivity.name)
        this.$set(item, 'activityType', item.cdkeyActivity.type === 1 ? '权益码' : item.cdkeyActivity.type === 2 ? '口令码' : item.cdkeyActivity.type === 3 ? '手机验证' : '')
        this.$set(item, 'createdAt', item.createdAt ? this.$u.timeFormat(item.createdAt) : '')

        // 切割couponNo字段为多行数据
        const couponNos = item.couponNo.split(',')
        item.couponNos = couponNos
      })

      // 表头&表数据
      const tHeader = ['活动名称', '活动类型', '权益码', '会员卡号', 'OPENID', '验证手机', '券号', '领取时间']
      const filterVal = ['activityName', 'activityType', 'cdkey', 'cardNo', 'openid', 'yz_mobile', 'couponNos', 'createdAt']

      const data = []

      list.forEach(v => {
        const rowData = filterVal.map(j => {
          if (j === 'couponNos') {
            return v[j]
          } else {
            return v[j]
          }
        })

        v.couponNos.forEach(couponNo => {
          const clonedRowData = [...rowData]
          clonedRowData[6] = couponNo// 更新券号字段为单个couponNo
          data.push(clonedRowData)
        })
      })

      this.$u.exports.excel('领取记录', tHeader, data)
    }
  }
}
</script>

<style scoped>

</style>
